package com.cz.nongyetongv5.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ProgressBar;

import com.cz.nongyetongv5.custom.wheelview.Utils;

import java.util.Random;

public class TextProgressBar extends ProgressBar {

	// 默认的text
	private String text = "";

	// 默认的文本颜色
	private int textColor = Color.parseColor("#36495C");

	// 默认的文本大小
	private float textSize = 15;

	/**
	 * 默认睡眠一秒钟
	 */
	public static final int randomProgressSleep = 1 * 1000;

	/**
	 * 是否执行executeThread
	 */
	private boolean executeRandomProgressThread = true;

	private ProgressListener progressListener = null;

	public TextProgressBar(Context context) {
		super(context);
	}

	public TextProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public TextProgressBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected synchronized void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		Paint textPaint = new Paint();

		textPaint.setAntiAlias(true);

		textPaint.setColor(textColor);

		textSize = Utils.sp2px(this.getContext(), 15);

		textPaint.setTextSize(textSize);

		Rect bounds = new Rect();

		textPaint.getTextBounds(text, 0, text.length(), bounds);

		int x = getWidth() / 2 - bounds.centerX();

		int y = getHeight() / 2 - bounds.centerY();

		canvas.drawText(text, x, y, textPaint);
	}

	@Override
	public synchronized void setProgress(int progress) {
		super.setProgress(progress);
	}

	public String getText() {
		return text;
	}

	public synchronized void setText(String text) {
		if (text != null) {
			this.text = text;
		} else {
			this.text = "";
		}

		postInvalidate();
	}

	public int getTextColor() {
		return textColor;
	}

	public synchronized void setTextColor(int textColor) {
		this.textColor = textColor;

		postInvalidate();
	}

	public float getTextSize() {
		return textSize;
	}

	public synchronized void setTextSize(float textSize) {
		this.textSize = textSize;
		postInvalidate();
	}

	/**
	 * 停止随机progress
	 */
	public synchronized void stopRandomProgress() {
		executeRandomProgressThread = false;
	}

	/**
	 * 开始随机progress
	 */
	public synchronized void startRandomProgress() {

		final Random random = new Random();
		// 随机增加progress
		Thread pushRandomProgress = new Thread() {

			@Override
			public void run() {

				while (true) {

					int progress = random.nextInt(2) + 1;

					int currentProgress = TextProgressBar.this.getProgress()
							+ progress;

					if (currentProgress >= 100) {

						TextProgressBar.this.setProgress(100);

						executeRandomProgressThread = false;

						if (progressListener != null) {

							progressListener.onExecuteFinish();
						}

						break;
					}

					TextProgressBar.this.setProgress(currentProgress);

					TextProgressBar.this.setText(currentProgress + "%");
					try {
						// 休息一段时间
						this.sleep(randomProgressSleep);
					} catch (InterruptedException e) {
					}

				}

			};

		};

		pushRandomProgress.start();

	}

	public ProgressListener getProgressListener() {
		return progressListener;
	}

	public void setProgressListener(ProgressListener progressListener) {
		this.progressListener = progressListener;
	}

	public interface ProgressListener {

		public void onExecuteFinish();
	}

}